package org.pictual.controller.mainPackage;

import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;

public class SpanishWikiDicParser extends WikiDicParser {

    private static List<String> _types = Arrays.asList(
            "Sustantivo femenino",
            "Sustantivo masculino",
            "Sustantivo",
            "Substantivo",
            "Sustantivo propio",
            "Verbo",
            "Verbo intransitivo",
            "Verbo transitivo",
            "Verbo transitivo e instransitivo",
            "Adjetivo",
            "Conjunción",
            "Adverbio",
            "Pronombre"
            );

    private static List<String> _langs = Arrays.asList(
            "Ingl.C3.A9s",
            "Espa.C3.B1ol"
            );

    protected boolean isLang(String language) {
        return Language.isLang(1, language);
    }

    protected boolean IsType(String word) {
        return _types.contains(word);
    }

    public String WikiLang(int lang) {
        return _langs.get(lang);
    }

    public void extractAudio(String line, DefinitionList listdef){
        line = line.substring(line.indexOf("<img"));
        line = line.substring(line.indexOf("<a"));
        //String id = line.substring(line.indexOf("Audio"), line.indexOf("</td>"));
        String extension;
        int index = line.indexOf(".ogg");
        if(index < 0){
            index = line.indexOf(".OGG");
            extension = ".OGG";
        } else{
            extension = ".ogg";
        }
        String link = line.substring(line.indexOf("http"), index);
        link = "href=\""+link+extension+"\" class=\"internal\" title=\"Audio"+listdef.getAudios().size()+"\" target='audioZone'";
        System.out.println("AUDIO: "+link);
        listdef.addAudio(link);
    }

    public void extractPronunciation(String line, DefinitionList listdef){
        String tmpPronunciation = "<br><br>";
        int index1 = line.indexOf("<li>");
        int index2 = line.indexOf("</li>");
        while(index1 >=0 && index2 >=0){
            String element = line.substring(index1, index2);
            if(element.contains("<img")){
                extractAudio(element, listdef);
            }
            if(element.contains("IPA") && tmpPronunciation.length() < 9){
                int i1, i2;
                String ref = element.substring(element.indexOf("IPA\">")+5);
                ref = ref.substring(0, ref.indexOf("<"));
                if(ref.indexOf("/") >= 0){
                    ref = ref.substring(ref.indexOf("/")+1);
                    ref = ref.substring(0, ref.indexOf("/"));
                }
                if(ref.indexOf("[") >= 0){
                    ref = ref.substring(ref.indexOf("[")+1);
                    ref = ref.substring(0, ref.indexOf("]"));
                }
                tmpPronunciation += ref;
                //System.out.println("Ex:"+ref);
                listdef.setPronunciation(tmpPronunciation);
            }
                line = line.substring(index2+5);
                index1 = line.indexOf("<li>");
                /*if(index1 >=0){
                    line = line.substring(index1);
                }*/
                index2 = line.indexOf("</li>");
                if(index1 > index2){
                    line = line.substring(index2+5);
                    index1 = line.indexOf("<li>");
                    index2 = line.indexOf("</li>");
                }
        }
    }

    public void extractDefinitions(String line, DefinitionList listdef){
        String ambito;
        String uso;
        String tmpDefinition = "";
        int index1 = line.indexOf("<dd>");
        int index2 = line.indexOf("</dd>")+5;
        while(index1 >=0 && index2 >=0){
            ambito = "";
            uso = "";
            String element = line.substring(index1+4, index2);
            if(element.contains("<ul>")){
            tmpDefinition = element.substring(0, element.indexOf("<ul>"));
            int index1a = element.indexOf("<li>");
            int index2a = element.indexOf("</li>")+5;
            while(index1a >=0 && index2a >=0){
                String ejemplo = element.substring(index1a, index2a);
                if(ejemplo.contains("<small>") && ejemplo.contains("</small>")){
                    String ref = ejemplo.substring(ejemplo.indexOf("<small>"), ejemplo.indexOf("</small>"));
                    ejemplo = ejemplo.replace(ref, "");
                }
                if(ejemplo.contains("Ejemplo:") || ejemplo.contains("Ejemplo ")){
                    if(element.contains("<dl>")){
                        ejemplo = line.substring(line.indexOf("Ejemplo"));
                        ejemplo = ejemplo.substring(ejemplo.indexOf("<dd>")+4, ejemplo.indexOf("</dd>"));
                    } else{
                        ejemplo = ejemplo.substring(ejemplo.indexOf("</b>")+5, ejemplo.indexOf("</li>"));
                    }
                    if(ejemplo.contains("(") && ejemplo.contains(")")){
                        String ref = ejemplo.substring(ejemplo.indexOf("("), ejemplo.indexOf(")")+1);
                        ejemplo = ejemplo.replace(ref, "");
                    }
                    if(ejemplo.contains("[") && ejemplo.contains("]")){
                        String ref = ejemplo.substring(ejemplo.indexOf("["), ejemplo.indexOf("]")+1);
                        ejemplo = ejemplo.replace(ref, "");
                    }
                    listdef.addExample(ejemplo);
                }
                if(ejemplo.contains("Ejemplos:")){
                    int index1b, index2b;
                    if(element.contains("<dl>")){
                        ejemplo = line.substring(line.indexOf("Ejemplo:"));
                        ejemplo = ejemplo.substring(ejemplo.indexOf("<dl>"), ejemplo.indexOf("</dl>"));
                        index1b = ejemplo.indexOf("<dd>")+4;
                        index2b = ejemplo.indexOf("</dd>");
                        while(index1b >=0 && index2b >=0){
                            String ejemplo2 = ejemplo.substring(index1b, index2b);
                            if(ejemplo2.contains("(") && ejemplo2.contains(")")){
                                String ref = ejemplo2.substring(ejemplo2.indexOf("("), ejemplo2.indexOf(")")+1);
                                ejemplo2 = ejemplo2.replace(ref, "");
                            }
                            if(ejemplo2.contains("[") && ejemplo2.contains("]")){
                                String ref = ejemplo.substring(ejemplo2.indexOf("["), ejemplo2.indexOf("]")+1);
                                ejemplo2 = ejemplo2.replace(ref, "");
                            }
                            listdef.addExample(ejemplo2);
                            ejemplo = ejemplo.substring(index2b+5);
                            index1b = ejemplo.indexOf("<dd>")+4;
                            index2b = ejemplo.indexOf("</dd>");
                        }
                    }else{
                        ejemplo = element.substring(element.indexOf("Ejemplos:"));
                        index1b = ejemplo.indexOf("<li>")+4;
                        index2b = ejemplo.indexOf("</li>");
                        while(index1b >=0 && index2b >=0){
                            String ejemplo2 = ejemplo.substring(index1b, index2b);
                            if(ejemplo2.contains("(") && ejemplo2.contains(")")){
                                String ref = ejemplo2.substring(ejemplo2.indexOf("("), ejemplo2.indexOf(")")+1);
                                ejemplo2 = ejemplo2.replace(ref, "");
                            }
                            if(ejemplo2.contains("[") && ejemplo2.contains("]")){
                                String ref = ejemplo.substring(ejemplo2.indexOf("["), ejemplo2.indexOf("]")+1);
                                ejemplo2 = ejemplo2.replace(ref, "");
                            }
                            listdef.addExample(ejemplo2);
                            ejemplo = ejemplo.substring(index2b+5);
                            index1b = ejemplo.indexOf("<li>");
                            index2b = ejemplo.indexOf("</li>")+5;
                        }
                    }
                }
                if(ejemplo.contains("ámbito:")){
                    ambito = ejemplo.substring(ejemplo.indexOf("</b>")+5, ejemplo.indexOf("</li>"));
                }
                if(ejemplo.contains("Uso:")){
                    uso = ejemplo.substring(ejemplo.indexOf("</b>")+5, ejemplo.indexOf("</li>"));
                }
                element = element.substring(index2a);
                index1a = element.indexOf("<li>");
                index2a = element.indexOf("</li>")+5;
            }
            }else{
                tmpDefinition = element.substring(0, element.indexOf("</dd>"));
            }
            line = line.substring(index2);
            index1 = line.indexOf("<dd>");
            index2 = line.indexOf("</dd>")+5;
            if(!tmpDefinition.startsWith("<b>ámbito</b>:")){
                if(!ambito.equals("") && !uso.equals("")){
                    tmpDefinition += " ("+ ambito +", " + uso +")";
                }
                if(!ambito.equals("") && uso.equals("")){
                    tmpDefinition += " ("+ ambito +")";
                }
                if(ambito.equals("") && !uso.equals("")){
                    tmpDefinition += " ("+ uso +")";
                }
                listdef.addDefinition(tmpDefinition);
            }
         }
    }

    public void extractGender(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("class=\"gender"));
            String gender = "";
            char option = line2.charAt(14);
            switch(option){
                case 'm':
                    gender = "der";
                    break;
                case 'f':
                    gender = "die";
                    break;
                case 'n':
                    gender = "das";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public void extractPlural(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("class=\"form-of plural"));
            String plural = line2.substring(line2.indexOf("title="));
            plural = plural.substring(7, plural.indexOf(">")-1);
            listdef.setPlural(plural);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    protected void extractEnglish(String line, DefinitionList listdef){
        if(line.contains("PronunciaciÃ³n:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("PronunciaciÃ³n:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        if(line.contains("Pronunciación:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Pronunciación:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains(">"+_types.get(i)+"<")){
                index += line2.indexOf(">"+_types.get(i)+"<")+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verbo"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("<dl>"));
                line2 = line2.substring(0, line2.indexOf("<h"));
                extractDefinitions(line2, listdef);
            }
        }
    }

    protected void extractSpanish(String line, DefinitionList listdef){
        if(line.contains("PronunciaciÃ³n:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("PronunciaciÃ³n:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        if(line.contains("Pronunciación:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Pronunciación:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains(">"+_types.get(i)+"<")){
                index += line2.indexOf(">"+_types.get(i)+"<")+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verbo"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                if(line2.contains("Sustantivo masculino")){
                    listdef.setGender("el");
                }
                if(line2.contains("Sustantivo femenino")){
                    listdef.setGender("la");
                }
                line2 = line2.substring(line2.indexOf("<dl>"));
                line2 = line2.substring(0, line2.indexOf("<h"));
                extractDefinitions(line2, listdef);
            }
        }
        if(listdef.isPlural()){
            if(listdef.getGender().equals("la")){
                listdef.setGender("las");
            } else{
                listdef.setGender("los");
            }
        }
    }

    protected void extractGerman(String line, DefinitionList listdef){
        if(line.contains("PronunciaciÃ³n:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("PronunciaciÃ³n:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        if(line.contains("Pronunciación:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Pronunciación:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains(">"+_types.get(i)+"<")){
                index += line2.indexOf(">"+_types.get(i)+"<")+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verbo"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("<dl>"));
                line2 = line2.substring(0, line2.indexOf("<h"));
                extractDefinitions(line2, listdef);
            }
        }
        if(listdef.isPlural()){
            listdef.setGender("pl. die");
        }
    }

    protected void extractFrench(String line, DefinitionList listdef){
        if(line.contains("PronunciaciÃ³n:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("PronunciaciÃ³n:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        if(line.contains("Pronunciación:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Pronunciación:"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation("<li>"+line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains(">"+_types.get(i)+"<")){
                index += line2.indexOf(">"+_types.get(i)+"<")+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verbo"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                if(line2.contains("Sustantivo masculino")){
                    listdef.setGender("le");
                }
                if(line2.contains("Sustantivo femenino")){
                    listdef.setGender("la");
                }
                line2 = line2.substring(line2.indexOf("<dl>"));
                line2 = line2.substring(0, line2.indexOf("<h"));
                extractDefinitions(line2, listdef);
            }
        }
        if(listdef.isPlural()){
            listdef.setGender("les");
        }
    }

    @Override
    protected boolean extractDefinitionList(BufferedReader input, DefinitionList listdef, int lang) {

        boolean end = false;
        boolean find = false;
        boolean begin = false;
        String languaje = Language.LanStr(1, lang);

        String line;

        try {
        	System.out.println("--------------------------------------");
        	System.out.println("we are "+languaje+" to spanish");
        	System.out.println("--------------------------------------");

            while (!end && (line = input.readLine()) != null) {
                while(!begin && !line.contains("</h1>")){
                    line = input.readLine();
                }
                if(!begin){
                    line = input.readLine();
                }
                begin = true;
            	if(line.contains("id=\""+languaje))
            	{
            		System.out.println("Spanish>>>"+line+"\n");
                        String line2 = "";
                        line = input.readLine();
                        while(line != null && !line.startsWith("<h1>")){
                            line2 += line;
                            line = input.readLine();
                        }
                        if(!line2.contains("id=\""+languaje)){
                            line = input.readLine();
                            line = input.readLine();
                            while(line != null && !line.startsWith("<h1>")){
                                line2 += line;
                                line = input.readLine();
                            }
                        }
                        line2 +="<h1>";
                        end = true;
                        find = true;
                        line2 = cambiar(line2);
                        switch(lang){
                            case 0:
                                extractEnglish(line2, listdef);
                                break;
                            case 1:
                                extractSpanish(line2, listdef);
                                break;
                            case 2:
                                extractGerman(line2, listdef);
                                break;
                            case 3:
                                extractFrench(line2, listdef);
                                break;
                            default:
                                find = false;
                        }
            	}
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return find;
    }

    public String cambiar(String line2){
        line2 = line2.replaceAll("Ã³", "ó");        
        line2 = line2.replaceAll("Ãº", "ú");
        line2 = line2.replaceAll("Ã±", "ñ");
        line2 = line2.replaceAll("Ã¡", "á");
        line2 = line2.replaceAll("Ã©", "é");
        line2 = line2.replaceAll("Ã�", "�?");
        line2 = line2.replaceAll("Ã", "í");
        return line2;
    }
}
